package fleditor.core.loader
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;
	
	public class BaseLoader extends EventDispatcher
	{
		protected var _id:int;
		protected var _urlLoader:URLLoader;
		protected var _urlRequest:URLRequest;
		protected var _urlVariables:URLVariables;
		protected var _requestMethod:String;
		protected var _url:String;
		protected var _isComplete:Boolean;
		protected var _isSuccess:Boolean;
		protected var _isLoading:Boolean;
		protected var _progressPercent:Number;
		protected var _repeat:int;
		protected var _currentRepeat:int=1;
		protected var _callBack:Function;
		protected var _transferData:*;
		protected var _autoExecute:Boolean;
		
		public function BaseLoader(id:int, url:String, autoExecute:Boolean=true, callBack:Function=null, repeat:int=3, urlVariables:URLVariables=null, requestMethod:String=URLRequestMethod.GET)
		{
			_id=id;
			_url=url;
			_autoExecute=autoExecute;
			_repeat=repeat;
			_callBack=callBack;
			_urlVariables=urlVariables;
			_requestMethod=requestMethod;
			_urlLoader=new URLLoader();
			
			addEvent();
			
			if(autoExecute) execute();
		}
		
		/**
		 *增加事件 
		 */		
		protected function addEvent():void
		{
			_urlLoader.addEventListener(Event.COMPLETE, __onCompleted);
			_urlLoader.addEventListener(ProgressEvent.PROGRESS, __onProgress);
			_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, __onHttpStatus);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, __onError);
			_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, __onError);
		}
		
		/**
		 *移除事件 
		 */		
		protected function removeEvent():void
		{
			_urlLoader.removeEventListener(Event.COMPLETE, __onCompleted);
			_urlLoader.removeEventListener(ProgressEvent.PROGRESS, __onProgress);
			_urlLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, __onHttpStatus);
			_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, __onError);
			_urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, __onError);
		}
		
		/**
		 * 执行加载(如果直接执行加载,其加载将不会载入加载列表,也将会忽略自身在缓存中的已加载项)
		 */
		public function execute():void
		{
			_urlLoader.dataFormat = getDataFormat();
			_urlRequest=new URLRequest();
			_urlRequest.url=_url;
			_urlRequest.data=_urlVariables;
			_urlRequest.method=_requestMethod;
			_isLoading=true;
			_isComplete=false;
			_isSuccess=false;
			_urlLoader.load(_urlRequest);
		}
		
		/**
		 *加载成功事件
		 */
		protected function __onCompleted(evt:Event):void
		{
			removeEvent();
			_urlLoader.close();
			
			_progressPercent=1;
			_isComplete=true;
			_isLoading=false;
			_isSuccess=true;
			
			dispatchEvent(new LoaderEvent(LoaderEvent.PROGRESS,this));
			dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE,this));
			
			if(_callBack!=null) _callBack(this);
		}
		
		/**
		 *加载错误事件
		 */
		protected function __onError(evt:Event):void
		{
			_isComplete=true;
			_isLoading=false;
			_isSuccess=false;
			
			loadError();
		}
		
		/**
		 *加载进度事件
		 */		
		protected function __onProgress(evt:ProgressEvent):void
		{
			_progressPercent = evt.bytesLoaded/evt.bytesTotal;
			dispatchEvent(new LoaderEvent(LoaderEvent.PROGRESS,this));
		}
		
		/**
		 *当前Http状态事件
		 */		
		protected function __onHttpStatus(evt:HTTPStatusEvent):void
		{
			if(evt.status > 399)
			{
				loadError();
			}
			
			dispatchEvent(new LoaderEvent(LoaderEvent.HTTP_STATUS,this));
		}
		
		/**
		 *加载错误处理 
		 */		
		protected function loadError():void
		{
			if(_currentRepeat < _repeat)
			{
				_currentRepeat++;
				_isLoading = false;
				_isComplete=false;
				_isSuccess=false;
				execute();
			}
			else
			{
				dispose();
				_isComplete = true;
				_isLoading = false;
				_isSuccess = false;
				dispatchEvent(new LoaderEvent(LoaderEvent.LOAD_ERROR,this));
				dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE,this));
			}
		}
		
		protected function getDataFormat():String
		{
			return URLLoaderDataFormat.BINARY;
		}
		
		public function dispose():void
		{
			removeEvent();
			_transferData=null;
			_urlRequest=null;
			_urlVariables=null;
			_urlLoader.data=null;
			_urlLoader=null;
		}
		
		/**
		 *是否正在加载中 
		 */		
		public function get isLoading():Boolean
		{
			return _isLoading;
		}
		
		/**
		 *是否加载成功 
		 */		
		public function get isSuccess():Boolean
		{
			return _isSuccess;
		}
		
		/**
		 *是否已执行完加载(并不代表加载成功) 
		 */		
		public function get isComplete():Boolean
		{
			return _isComplete;
		}
		
		/**
		 *要加载的URL 
		 */		
		public function get url():String
		{
			return _url;
		}
		
		/**
		 *加载项分配的ID 
		 */		
		public function get id():int
		{
			return _id;
		}
		
		/**
		 *加载后的数据 
		 */		
		public function get data():*
		{
			return _urlLoader.data;
		}
		
		/**
		 *加载进度百分比 
		 */		
		public function get progressPercent():Number
		{
			return _progressPercent;
		}
		
		/**
		 *取得失败后,重复加载次数 
		 */		
		public function get repeat():int
		{
			return _repeat;
		}

		/**
		 *需要传递的数据(方便使用,不会对该数据做任何处理)
		 */		
		public function get transferData():*
		{
			return _transferData;
		}

		/**
		 *需要传递的数据(方便使用,不会对该数据做任何处理)
		 */
		public function set transferData(value:*):void
		{
			_transferData = value;
		}

		/**
		 *是否创建成功后,立即执行加载 
		 */		
		public function get autoExecute():Boolean
		{
			return _autoExecute;
		}

	}
}